﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using SelectLib;

namespace CAS原子操作
{
    class Program
    {

        class A
        {
            public int num = 0;
            public A(int i) { num = i; }

        }

        static TMsgQueue<A> msgQueue = new TMsgQueue<A>();
        static bool f1 = false;
        static bool f2 = false;
        static bool f3 = false;
        static bool endflag = false;
        
        static void Main(string[] args)
        {

            //int val = 1;
            //int ret = Interlocked.Exchange(ref val, 0);

            //Console.WriteLine("val=" + val.ToString() + " ,ret=" + ret.ToString());

            //ret = Interlocked.Exchange(ref val, 1);

            //Console.WriteLine("val=" + val.ToString() + " ,ret=" + ret.ToString());

            //ret = Interlocked.Exchange(ref val, 2);

            //Console.WriteLine("val=" + val.ToString() + " ,ret=" + ret.ToString());

            //ret = Interlocked.Exchange(ref val, 3);

            //Console.WriteLine("val=" + val.ToString() + " ,ret=" + ret.ToString());

            //ret = Interlocked.Exchange(ref val, val);

            //Console.WriteLine("val=" + val.ToString() + " ,ret=" + ret.ToString());
            
            Console.WriteLine("程序启动 " + DateTime.Now);
            DateTime dt1 = DateTime.Now;
            Thread[] tharr = new Thread[4];
            for(int i = 0; i < tharr.Length; i++)
            {
                tharr[i] = new Thread(ThreadFun);
                tharr[i].IsBackground = true;
                tharr[i].Start(i.ToString());
            }
            while (endflag != true) Thread.Sleep(1);
            DateTime dt2 = DateTime.Now;
            TimeSpan ts = dt2 - dt1;
            Console.WriteLine("程序总计运行时间：" + ts.TotalMilliseconds + " ms");
            Console.ReadKey();
        }

        static private void ThreadFun(object num)
        {
            int flag = Convert.ToInt32(num);

            if(flag == 0)
            {
                int total = 10000000;
                for(int i=0;i< total; i++)
                {
                    msgQueue.InQueue(new A(i + 1));
                }
                f1 = true;
                Console.WriteLine("flag=" + flag.ToString() + " 共存入数据：" + total.ToString() + "  " + DateTime.Now);
            }
            else if(flag == 1) {
                int total = 10000000;
                for (int i = 0; i < total; i++)
                {
                    msgQueue.InQueue(new A(i + 1));
                }
                f2 = true;
                Console.WriteLine("flag=" + flag.ToString() + " 共存入数据：" + total.ToString() + "  " + DateTime.Now);
            }
            else if(flag == 2) {
                int total = 10000000;
                for (int i = 0; i < total; i++)
                {
                    msgQueue.InQueue(new A(i + 1));
                }
                f3 = true;
                Console.WriteLine("flag=" + flag.ToString() + " 共存入数据：" + total.ToString() + "  " + DateTime.Now);
            }
            else
            {
                int count = 0;
                int queueCnt = 0;
                do
                {
                    Thread.Sleep(1);
                    queueCnt = msgQueue.msgQueue.Count;
                    for (int i = 0; i < queueCnt; i++)
                    {
                        A a = msgQueue.OutQueue();
                        //Console.WriteLine(a.num);
                        count++;
                    }
                    Thread.Sleep(20);
                    queueCnt = msgQueue.msgQueue.Count;
                } while (f1 == false || f2 == false || f3 == false || queueCnt>0);
                endflag = true;
                Console.WriteLine("共取出数据：" + count.ToString() + " " + DateTime.Now);
            }

        }


        //static void CASThread()
        //{
        //    for(int i=0;i<3;)

        //}
    }
}
